package com.example.appframework.ui.person;

import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appframework.MainActivity;
import com.example.appframework.R;
import com.example.appframework.login.LoginMainActivity;

public class PersonFragment extends Fragment {

    private PersonViewModel mViewModel;
    private String username;

    Button loginButton;
    TextView textUsername;

    public static PersonFragment newInstance() {
        return new PersonFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.person_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(PersonViewModel.class);
        // TODO: Use the ViewModel

        loginButton = getView().findViewById(R.id.loginButton);
        textUsername = getView().findViewById(R.id.textUsername);

        if(username == null){
            loginButton.setVisibility(View.VISIBLE);
            textUsername.setText("当前未登录");
        }
        else {
            loginButton.setVisibility(View.INVISIBLE);
            textUsername.setText("当前用户: " + username);
        }

        Log.i("show", "person:" + username);

        //进入setting页面
        getView().findViewById(R.id.settingButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                        .setTitle(username == null ? "尚未登录" : "确定要退出登录吗？")//标题
                        //.setMessage("这是内容")//内容
                        .setIcon(R.mipmap.ic_launcher)//图标
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                username = null;
                                loginButton.setVisibility(View.VISIBLE);
                                textUsername.setText("当前未登录");
                                reset(getActivity());
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create();
                alertDialog.show();
            }
        });

        getView().findViewById(R.id.loginButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginMainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        username = ((MainActivity) activity).getUsername();
    }

    private void reset(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).resetUsername();
    }
}